package net.xdclass.service.stress.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.dto.stress.StressCaseDTO;
import net.xdclass.dto.stress.StressCaseModuleDTO;
import net.xdclass.mapper.StressCaseMapper;
import net.xdclass.mapper.StressCaseModuleMapper;
import net.xdclass.model.StressCaseDO;
import net.xdclass.model.StressCaseModuleDO;
import net.xdclass.req.stress.StressCaseModuleSaveReq;
import net.xdclass.req.stress.StressCaseModuleUpdateReq;
import net.xdclass.service.stress.StressCaseModuleService;
import net.xdclass.util.SpringBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小二
 */
@Service
@Slf4j
public class StressCaseModuleServiceImpl implements StressCaseModuleService {

    @Resource
    private StressCaseModuleMapper caseModuleMapper;

    @Resource
    private StressCaseMapper stressCaseMapper;

    /**
     * 根据项目ID查询压测模型及其关联用例列表
     *
     * @param projectId 项目ID
     * @return 压测模型及其关联用例列表
     */
    @Override
    public List<StressCaseModuleDTO> list(Long projectId) {

        // 创建查询条件
        LambdaQueryWrapper<StressCaseModuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseModuleDO::getProjectId, projectId);
        // 查询压测模型
        List<StressCaseModuleDO> stressCaseModuleDOS = caseModuleMapper.selectList(queryWrapper);
        // 将压测模型实体转换为传输对象
        List<StressCaseModuleDTO> list = SpringBeanUtil.copyProperties(stressCaseModuleDOS, StressCaseModuleDTO.class);
        // 遍历每个压测模型，查询并关联其用例
        list.forEach(source -> {
            // 创建查询用例的条件
            LambdaQueryWrapper<StressCaseDO> caseQueryWrapper = new LambdaQueryWrapper<>();
            caseQueryWrapper.eq(StressCaseDO::getModuleId, source.getId()).orderByDesc(StressCaseDO::getId);
            // 查询关联用例
            List<StressCaseDO> stressCaseDOS = stressCaseMapper.selectList(caseQueryWrapper);
            // 将用例实体转换为传输对象
            List<StressCaseDTO> stressCaseDTOS = SpringBeanUtil.copyProperties(stressCaseDOS, StressCaseDTO.class);
            // 将关联用例添加到压测模型中
            source.setList(stressCaseDTOS);
        });
        // 返回压测模型及其关联用例列表
        return list;
    }

    /**
     * 根据项目ID和模型ID查询压测模型及其关联用例详情
     *
     * @param projectId 项目ID
     * @param moduleId 模型ID
     * @return 压测模型及其关联用例详情
     */
    @Override
    public StressCaseModuleDTO findById(Long projectId, Long moduleId) {
        // 创建查询条件
        LambdaQueryWrapper<StressCaseModuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseModuleDO::getProjectId, projectId).eq(StressCaseModuleDO::getId, moduleId);
        // 查询压测模型
        StressCaseModuleDO stressCaseModuleDO = caseModuleMapper.selectOne(queryWrapper);
        // 如果模型不存在，返回null
        if (stressCaseModuleDO == null) {
            return null;
        }
        // 将压测模型实体转换为传输对象
        StressCaseModuleDTO stressCaseModuleDTO = SpringBeanUtil.copyProperties(stressCaseModuleDO, StressCaseModuleDTO.class);

        // 创建查询用例的条件
        LambdaQueryWrapper<StressCaseDO> caseQueryWrapper = new LambdaQueryWrapper<>();
        caseQueryWrapper.eq(StressCaseDO::getModuleId, moduleId).orderByDesc(StressCaseDO::getId);
        // 查询关联用例
        List<StressCaseDO> stressCaseDOS = stressCaseMapper.selectList(caseQueryWrapper);
        // 将用例实体转换为传输对象
        List<StressCaseDTO> stressCaseDTOS = SpringBeanUtil.copyProperties(stressCaseDOS, StressCaseDTO.class);
        // 将关联用例添加到压测模型中
        stressCaseModuleDTO.setList(stressCaseDTOS);
        // 返回压测模型及其关联用例详情
        return stressCaseModuleDTO;
    }

    /**
     * 删除压测模型及其关联用例
     *
     * @param projectId 项目ID
     * @param id 模型ID
     * @return 删除结果，大于0表示删除成功
     */
    @Override
    public int delete(Long projectId, Long id) {
        // 创建查询条件
        LambdaQueryWrapper<StressCaseModuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseModuleDO::getProjectId, projectId).eq(StressCaseModuleDO::getId, id);
        // 删除压测模型
        int delete = caseModuleMapper.delete(queryWrapper);
        // 如果模型删除成功，删除其关联用例
        if (delete > 0) {
            LambdaQueryWrapper<StressCaseDO> caseQueryWrapper = new LambdaQueryWrapper<>();
            caseQueryWrapper.eq(StressCaseDO::getModuleId, id);
            stressCaseMapper.delete(caseQueryWrapper);
        }
        // 返回删除结果
        return delete;
    }

    /**
     * 保存压测模型
     *
     * @param req 压测模型保存请求参数
     * @return 插入结果
     */
    @Override
    public int save(StressCaseModuleSaveReq req) {

        // 将请求参数转换为压测模型实体
        StressCaseModuleDO stressCaseModuleDO = SpringBeanUtil.copyProperties(req, StressCaseModuleDO.class);
        // 插入压测模型
        return caseModuleMapper.insert(stressCaseModuleDO);
    }

    /**
     * 更新压测模型
     *
     * @param req 压测模型更新请求参数
     * @return 更新结果，大于0表示更新成功
     */
    @Override
    public int update(StressCaseModuleUpdateReq req) {
        // 将请求参数转换为压测模型实体
        StressCaseModuleDO stressCaseModuleDO = SpringBeanUtil.copyProperties(req, StressCaseModuleDO.class);
        // 创建更新条件
        LambdaQueryWrapper<StressCaseModuleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseModuleDO::getProjectId, stressCaseModuleDO.getProjectId()).eq(StressCaseModuleDO::getId, stressCaseModuleDO.getId());
        // 更新压测模型
        return caseModuleMapper.update(stressCaseModuleDO, queryWrapper);
    }
}
